﻿Imports System
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml.Linq
Imports Exp.Model
Imports Exp.DATA_Service

Public Class rqBooking
    Inherits System.Web.UI.Page
    Private _User As REM_User

      Private Property user() As REM_User
        Get
            If Session("USER") Is Nothing Then Response.Redirect("~/login.aspx") Else _User = CType(Session("USER"), REM_User)
            Return _User
        End Get
        Set(value As REM_User)
            _User = value
        End Set
    End Property

    Const MENUNAME As String = "REQBOOK"
    Const SendReqSuccess As String = "Send request booking completed."
    Const SaveSuccess As String = "Save completed."
    Const SaveError As String = "Save error !"
    Const DeleteSuccess As String = "Delete completed."
    Const DeleteError As String = "Delete not complete"

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            If Session("USER") Is Nothing Then
                Response.Redirect("~/login.aspx")
            Else
                user = CType(Session("USER"), REM_User)

                Dim objUSV As New Exp.DATA_Service.User_Service
                If Not objUSV.isHasRight(user.User_ID, MENUNAME) Then
                    Session("USER") = Nothing
                    Session.Clear()
                    Response.Redirect("~/login.aspx")
                End If

                MultiView1.ActiveViewIndex = 0
                BindControl()
                BindGridview0()
            End If

            If isJoin() = False Then
                JavaScript.AlertMessage(Me.Page, "Your status is'n join Exchang Program.")
                Response.Redirect("rqJoin.aspx")
            End If
        End If

    End Sub

    Sub BindBrand()
        Dim REM_Brand As New List(Of REM_Brand)
        Dim Brand_Service As New Exp.DATA_Service.Exp.DATA_Service.Brand_Service
        REM_Brand = Brand_Service.GetAll()
        REM_Brand.Distinct()
        Me.ddlBrandView0.DataSource = REM_Brand
        Me.ddlBrandView0.DataTextField = "BrandName"
        Me.ddlBrandView0.DataValueField = "Brand_ID"
        Me.ddlBrandView0.DataBind()
        Me.ddlBrandView0.Items.Insert(0, New ListItem("", ""))

        Me.ddlBrandView1.DataSource = REM_Brand
        Me.ddlBrandView1.DataTextField = "BrandName"
        Me.ddlBrandView1.DataValueField = "Brand_ID"
        Me.ddlBrandView1.DataBind()
        Me.ddlBrandView1.Items.Insert(0, New ListItem("", ""))


    End Sub
    Sub BindCountry()
        Dim REM_Country As New List(Of REM_Country)
        Dim Country_service As New Country_Service
        REM_Country = Country_service.GetAll()
        REM_Country.Distinct()
        Me.ddlCountryView0.DataSource = REM_Country
        Me.ddlCountryView0.DataTextField = "CountryName"
        Me.ddlCountryView0.DataValueField = "Country_ID"
        Me.ddlCountryView0.DataBind()
        Me.ddlCountryView0.Items.Insert(0, New ListItem("", ""))

        Me.ddlCountryView1.DataSource = REM_Country
        Me.ddlCountryView1.DataTextField = "CountryName"
        Me.ddlCountryView1.DataValueField = "Country_ID"
        Me.ddlCountryView1.DataBind()
        Me.ddlCountryView1.Items.Insert(0, New ListItem("", ""))

    End Sub
    Sub BindResort()
        Dim REM_Resort As New List(Of REM_Resort)
        Dim Resort_service As New Resort_service
        REM_Resort = Resort_service.GetAll()
        REM_Resort.Distinct()
        Me.ddlResortView0.DataSource = REM_Resort
        Me.ddlResortView0.DataTextField = "ResortName"
        Me.ddlResortView0.DataValueField = "Resort_ID"
        Me.ddlResortView0.DataBind()
        Me.ddlResortView0.Items.Insert(0, New ListItem("", ""))

        Me.ddlResortView1.DataSource = REM_Resort
        Me.ddlResortView1.DataTextField = "ResortName"
        Me.ddlResortView1.DataValueField = "Resort_ID"
        Me.ddlResortView1.DataBind()
        Me.ddlResortView1.Items.Insert(0, New ListItem("", ""))
    End Sub

    Private Function isJoin() As Boolean
        Dim result As Boolean = False

        If Not user.JoinDate Is Nothing AndAlso Not user.ExpireDate Is Nothing Then
            If Now.Date >= user.JoinDate.Value AndAlso Now.Date <= user.ExpireDate.Value Then
                result = True
            End If
        End If


        Return result

    End Function
    Private Sub BindControl()

        Dim itemAll As New ListItem("", "")
        Dim itemActive As New ListItem("Active", "1")
        Dim itemInactive As New ListItem("Inactive", "0")

        Dim objSV As New Owner_Service

        Dim iUnit As Integer = 0
        Dim iCountUnit = objSV.GetByUser_ID(user.User_ID).Count
        Me.lblRequestorView0.Text = user.TitleName & "" & user.FirstName & " " & user.LastName
        Me.lblRequestorView1.Text = Me.lblRequestorView0.Text
        Me.lblRequestorView2.Text = Me.lblRequestorView0.Text

        If Not user.JoinDate Is Nothing Then
            Me.lblJoinDateView0.Text = user.JoinDate.Value.ToString("dd MMM yyyy")
            Me.lblJoinDateView1.Text = Me.lblJoinDateView0.Text
            Me.lblJoinDateView2.Text = Me.lblJoinDateView0.Text
        End If

        If Not user.ExpireDate Is Nothing Then
            Me.lblExpireDateView0.Text = user.ExpireDate.Value.ToString("dd MMM yyyy")
            Me.lblExpireDateView1.Text = Me.lblExpireDateView0.Text
            Me.lblExpireDateView2.Text = Me.lblExpireDateView0.Text
        End If

        Me.btnOwnerUnitView0.Text = iCountUnit.ToString

        BindBrand()
        BindCountry()
        BindResort()

        Dim objUserSV As New User_Service
        Me.lblPointBalanceView0.Text = objUserSV.GetPointBalance(user.User_ID).ToString("#,###") 'GetFullPoint()
        Me.lblPointBalanceView1.Text = Me.lblPointBalanceView0.Text
        Dim usrService As New User_Service()

    End Sub
    Private Sub BindGridview0()
        Dim objSV As New Booking_Service
        Dim listData = objSV.GetBookingDetail(0, user.User_ID)
        grdView0.DataSource = listData
        grdView0.DataBind()

    End Sub
  
    Protected Sub btnSearchView0_Click(sender As Object, e As EventArgs) Handles btnSearchView0.Click
        BindGridview0()
    End Sub
    Protected Sub imgAddView0_Click(sender As Object, e As EventArgs) Handles imgAddView0.Click
        MultiView1.ActiveViewIndex = 1
        ClearFormView1()
        'BindGridview1()
    End Sub
    Protected Sub grdView0_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles grdView0.RowCommand
        Dim commandArgs As String() = e.CommandArgument.ToString().Split(",")
        Dim Booking_ID As Integer = CInt(commandArgs(0))

        If e.CommandName.ToString.ToUpper = "EDIT" Then
            MultiView1.ActiveViewIndex = 1
            ClearFormView1()
            'BindDataForEdit(User_ID)
            'ElseIf e.CommandName.ToString.ToUpper = "DELETE" Then
            'Dim objSVUser As New User_Service
            'objSVUser.Delete(Booking_ID)
            'JavaScript.AlertMessage(Me.Page, DeleteSuccess)
            'Exit Sub
        ElseIf e.CommandName.ToString.ToUpper = "DETAIL" Then
            ClearFormView2()
            BindDataForView2(Booking_ID)
            MultiView1.ActiveViewIndex = 2
        End If
    End Sub
    Private Sub grdView0_RowEditing(sender As Object, e As GridViewEditEventArgs) Handles grdView0.RowEditing

    End Sub

    Protected Sub btnBackView3_Click(sender As Object, e As EventArgs) Handles btnBackView3.Click
        MultiView1.ActiveViewIndex = CInt(hdFromView3.Value)
    End Sub

#Region "View1"
    Protected Sub btnBackView1_Click(sender As Object, e As EventArgs) Handles btnBackView1.Click
        MultiView1.ActiveViewIndex = 0
    End Sub
    Private Sub ClearFormView1()
        Me.ddlCountryView1.SelectedIndex = 0
        Me.ddlBrandView1.SelectedIndex = 0
        Me.ddlResortView1.SelectedIndex = 0
        Me.txtLocationView1.Text = ""
        Me.txtCheckinDateView1.Text = ""
        Me.txtCheckoutDateView1.Text = ""
        Me.gv_dataView1.DataSource = Nothing
        Me.gv_dataView1.DataBind()
    End Sub
    Private Sub gv_dataView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gv_dataView1.RowCommand
        Dim unit_ID As Integer = CInt(e.CommandArgument.ToString.Trim)
        Me.hdUnit_ID_View2.Value = unit_ID
        If e.CommandName.ToUpper = "DETAIL" Then
            MultiView1.ActiveViewIndex = 3
            hdFromView3.Value = 1
            Me.ucUnitView3.UnitID = unit_ID
            Me.ucUnitView3.BindData()

        ElseIf e.CommandName.ToUpper = "SELECT" Then
            Me.lblTopicView2.Text = "Request Booking Step 2 : Verify detail"
            Me.pnlView.Visible = False
            Me.pnlSubmit.Visible = True

            Dim sError As String = ""
            If Me.txtCheckinDateView1.Text = "" Then sError += "- Check in date\n"
            If Me.txtCheckoutDateView1.Text = "" Then sError += "- Check out date\n"

            If sError.Trim <> "" Then
                sError = "Please verify.\n" & sError
                JavaScript.AlertMessage(Me.Page, sError)
                Exit Sub
            End If

            Dim startdate As DateTime = Date.ParseExact(Me.txtCheckinDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            Dim enddate As DateTime = Date.ParseExact(Me.txtCheckoutDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

            If enddate < startdate Then sError += "- Check in date not more than check out date\n"

            If sError.Trim <> "" Then
                JavaScript.AlertMessage(Me.Page, sError)
                Exit Sub
            End If

            Dim night As Integer = 0
            Dim usePoint As Integer = 0
            Dim svUnit As New Unit_Service
            Dim svUnitType As New UnitType_service


            Dim objUnit = svUnit.GetByID(unit_ID)
            Dim objUnitType = svUnitType.GetByID(objUnit.UnitType_ID)

            night = DateDiff(DateInterval.Day, startdate, enddate) + 1

            usePoint = night * objUnit.PointPerNight & ""

            MultiView1.ActiveViewIndex = 2
            Me.lblCheckinDateView2.Text = Me.txtCheckinDateView1.Text.Trim & ""
            Me.lblCheckoutDateView2.Text = Me.txtCheckoutDateView1.Text.Trim & ""
            Me.lblUnitNumberView2.Text = objUnit.UnitName.Trim & ""
            Me.lblUnitTypeView2.Text = objUnit.REM_UnitType.UnitTypeName & ""
            Me.lblUnitDescView2.Text = objUnit.Description & ""
            Me.lblUnitAddressView2.Text = objUnit.Address & ""
            Me.lblResortView2.Text = objUnit.REM_Resort.ResortName & ""
            Me.lblBrandView2.Text = objUnit.REM_Resort.REM_Brand.BrandName
            Me.lblCountryView2.Text = objUnit.REM_Resort.REM_Country.CountryName
            Me.lblRateView2.Text = objUnit.PointPerNight & ""
            Me.txtNightView2.Text = night.ToString
            Me.txtUsePointsView2.Text = usePoint.ToString
            Me.txtRemarkView2.Text = ""
            Me.txtRemarkView2.ReadOnly = False
        End If
    End Sub
    Private Sub btnSearchUnitView1_Click(sender As Object, e As EventArgs) Handles btnSearchUnitView1.Click
        Dim sError As String = ""
        If Me.txtCheckinDateView1.Text = "" Then sError += "- Check in date\n"
        If Me.txtCheckoutDateView1.Text = "" Then sError += "- Check out date\n"


        If sError.Trim <> "" Then
            sError = "Please verify.\n" & sError
            JavaScript.AlertMessage(Me.Page, sError)
            Exit Sub
        End If

        Dim startdate As DateTime = Date.ParseExact(Me.txtCheckinDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
        Dim enddate As DateTime = Date.ParseExact(Me.txtCheckoutDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

        If enddate < startdate Then
            sError += "- Check in date not more than check out date\n"
        End If


        'Dim listData = objSV.GetAll
        'If Me.ddlUsernameView0.SelectedIndex > 0 Then listData = listData.Where(Function(o) o.User_ID = CInt(Me.ddlUsernameView0.SelectedValue.Trim)).ToList
        'If Me.ddlRoleView0.SelectedIndex > 0 Then listData = listData.Where(Function(o) o.Role_ID = CInt(Me.ddlRoleView0.SelectedValue.Trim)).ToList

        'If Me.txtFirstNameView0.Text.Trim <> "" Then listData = listData.Where(Function(o) o.FirstName.ToUpper.Contains(Me.txtFirstNameView0.Text.ToUpper.Trim)).ToList
        'If Me.txtLastNameView0.Text.Trim <> "" Then listData = listData.Where(Function(o) o.LastName.ToUpper.Contains(Me.txtLastNameView0.Text.ToUpper.Trim)).ToList

        'If Me.txtMobilePhoneView0.Text.Trim <> "" Then listData = listData.Where(Function(o) o.MobilePhone.ToUpper.Contains(Me.txtMobilePhoneView0.Text.ToUpper.Trim)).ToList
        'If Me.txtEmailView0.Text.Trim <> "" Then listData = listData.Where(Function(o) o.Email.ToUpper.Contains(Me.txtEmailView0.Text.ToUpper.Trim)).ToList
        'If Me.txtAddressView0.Text.Trim <> "" Then listData = listData.Where(Function(o) o.Address.ToUpper.Contains(Me.txtAddressView0.Text.ToUpper.Trim)).ToList

        'If Me.ddlStatusView0.SelectedIndex > 0 Then
        '    Dim status As Boolean = IIf(Me.ddlStatusView0.SelectedValue.Trim = "1", True, False)
        '    listData = listData.Where(Function(o) o.isActive = status).ToList
        'End If

        'grdView0.DataSource = listData
        'grdView0.DataBind()
        If sError.Trim <> "" Then
            JavaScript.AlertMessage(Me.Page, sError)
            Exit Sub
        End If

        Dim objSV As New Owner_Service
        Dim listData = objSV.GetOwner_ByUserID(0).Where(Function(o) o.ShareStartDate.HasValue = True And o.ShareEndDate.HasValue = True)
        'listData = listData.Where(Function(o) o.ShareStartDate.Value <= startdate).ToList
        listData = listData.Where(Function(o) o.ShareStartDate.Value <= startdate And o.ShareEndDate.Value >= enddate).ToList


        If Me.ddlCountryView1.SelectedIndex > 0 Then
            Dim Country_ID As Integer = CInt(Me.ddlCountryView1.SelectedValue.Trim)
            listData = listData.Where(Function(o) o.Country_ID = Country_ID).ToList
        End If
        If Me.ddlBrandView1.SelectedIndex > 0 Then
            Dim Brand_ID As Integer = CInt(Me.ddlBrandView1.SelectedValue.Trim)
            listData = listData.Where(Function(o) o.Brand_ID = Brand_ID).ToList
        End If
        If Me.ddlResortView1.SelectedIndex > 0 Then
            Dim Resort_ID As Integer = CInt(Me.ddlResortView1.SelectedValue.Trim)
            listData = listData.Where(Function(o) o.Resort_ID = Resort_ID).ToList
        End If
        If Me.txtLocationView1.Text.Trim <> "" Then
            Dim Address As String = Me.txtLocationView1.Text.Trim
            listData = listData.Where(Function(o) o.Address.ToUpper.Contains(Address.ToUpper)).ToList
        End If

        gv_dataView1.DataSource = listData
        gv_dataView1.DataBind()


    End Sub
#End Region

#Region "View2"
    Private Sub BindDataForView2(ByVal Booking_ID As Integer)
        Dim objSV As New Booking_Service
        Dim objUSV As New Unit_Service
        Dim objData = objSV.GetBookingDetail(Booking_ID, 0).FirstOrDefault
        Dim unit_ID As Integer = objData.Unit_ID.Value
        Dim objUn = objUSV.GetByID(unit_ID)

        Me.lblCheckinDateView2.Text = objData.StartDate.Value.ToString("dd/MM/yyyy") 'Me.txtCheckinDateView1.Text.Trim & ""
        Me.lblCheckoutDateView2.Text = objData.EndDate.Value.ToString("dd/MM/yyyy")
        Me.lblUnitNumberView2.Text = objUn.UnitName.Trim
        Me.lblUnitTypeView2.Text = objData.UnitTypeName.Trim  'REM_UnitType.UnitTypeName & ""
        Me.lblUnitDescView2.Text = objData.Description & ""
        Me.lblUnitAddressView2.Text = objUn.Address & ""
        Me.lblResortView2.Text = objUn.REM_Resort.ResortName & ""
        Me.lblBrandView2.Text = objUn.REM_Resort.REM_Brand.BrandName & ""

        Me.lblCountryView2.Text = objUn.REM_Resort.REM_Country.CountryName 'objData.CountryName
        Me.lblRateView2.Text = objData.PointPerNight & ""
        Me.txtNightView2.Text = objData.Night.ToString & ""
        Me.txtUsePointsView2.Text = objData.Point.Value.ToString("#,###") & ""
        Me.txtRemarkView2.Text = objData.Remark.ToString
        Me.txtRemarkView2.ReadOnly = True
        Me.lblTopicView2.Text = "Booking detail"
        Me.pnlView.Visible = True
        Me.pnlSubmit.Visible = False
    End Sub
    Private Sub ClearFormView2()
        Me.lblCheckinDateView2.Text = ""
        Me.lblCheckoutDateView2.Text = ""
        Me.lblUnitNumberView2.Text = ""
        Me.lblUnitTypeView2.Text = ""
        Me.lblUnitDescView2.Text = ""
        Me.lblUnitAddressView2.Text = ""
        Me.lblResortView2.Text = ""
        Me.lblBrandView2.Text = ""
        Me.lblCountryView2.Text = ""
        Me.lblRateView2.Text = ""
        Me.txtNightView2.Text = ""
        Me.txtUsePointsView2.Text = ""
        Me.txtRemarkView2.Text = ""


    End Sub
    Private Sub lnkSubmitView2_Click(sender As Object, e As EventArgs) Handles lnkSubmitView2.Click
        Dim svBooking As New Booking_Service
        Dim objBooking As New RET_Booking
        Try

            Dim startdate As DateTime = Date.ParseExact(Me.txtCheckinDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)
            Dim enddate As DateTime = Date.ParseExact(Me.txtCheckoutDateView1.Text.Trim, "dd/MM/yyyy", System.Globalization.DateTimeFormatInfo.InvariantInfo)

            objBooking.Requester_ID = user.User_ID
            objBooking.Remark = Me.txtRemarkView2.Text.Trim
            objBooking.StartDate = startdate
            objBooking.EndDate = enddate
            objBooking.Status = "REQUEST"
            objBooking.CreateDate = Now
            objBooking.CreateUser = user.User_ID
            objBooking.isActive = True

            svBooking.Add(objBooking, CInt(Me.hdUnit_ID_View2.Value), Me.txtRemarkView2.Text.Trim, CInt(Me.txtNightView2.Text.Trim), CInt(Me.txtUsePointsView2.Text.Trim))

            MultiView1.ActiveViewIndex = 0
            BindGridview0()
            JavaScript.AlertMessage(Me.Page, SendReqSuccess)
        Catch ex As Exception
            JavaScript.AlertMessage(Me.Page, ex.Message)
            Exit Sub
        End Try

    End Sub
    Private Sub lnkBackView2_Click(sender As Object, e As EventArgs) Handles lnkBackView2.Click
        MultiView1.ActiveViewIndex = 1
    End Sub
    Protected Sub lnkBacktoFirstView2_Click(sender As Object, e As EventArgs) Handles lnkBacktoFirstView2.Click
        MultiView1.ActiveViewIndex = 0
    End Sub
#End Region
End Class